import sys
from components.page1 import PageOne
from components.page2 import PageTwo
from components.paged_table import PagedTable
from views.car.plate import PhotoView
from views.face.input import FaceView
from views.face.identify import FaceIdentifyView
from views.face.input_many import FaceManyView
from views.others.cook_book import CookBook
from views.others.users_view import Users
from components.login_in import LoginIn
from PyQt5.QtWidgets import QApplication, QMainWindow, QStackedWidget, QVBoxLayout, QLabel, QPushButton, QWidget, \
    QDesktopWidget, QAction, qApp
from PyQt5.QtCore import QRect, pyqtSlot
from components.menu import CustomMenu


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        # 保存登录窗口的引用
        self.login_in_widget = LoginIn()
        self.login_in_widget.data_login.connect(self.receive_value)
        # 堆叠窗口实现多窗口切换
        self.page1 = PageOne()
        # self.page1 = PagedTable('face_input_many')
        self.page2 = PageTwo()
        self.car_plate = PhotoView()
        self.face_input = FaceView()
        self.face_identity = FaceIdentifyView()
        self.face_input_many = FaceManyView()
        self.cook_book = CookBook()
        self.user_view = Users()
        self.page1.page_size.connect(self.page_change)
        self.page2.page_size.connect(self.page_change)
        self.stacked_widget = QStackedWidget(self)
        self.user = ''
        self.setWindowTitle("车辆识别")
        self.init_ui()

    def init_ui(self):
        # 菜单控件
        menu_bar = CustomMenu()
        self.setMenuBar(menu_bar)
        menu_bar.window_index.connect(self.page_change)

        # 设置中心部件
        self.setCentralWidget(self.stacked_widget)

        # 设置窗口的初始尺寸
        self.setGeometry(100, 100, 1280, 960)
        # 窗口居中
        # self.put_window_in_center()
        self.center()
        # 登录框阻断主窗口
        # self.show_login_window()

        # 堆叠窗口实现多窗口切换
        self.stacked_widget.addWidget(self.page1)
        self.stacked_widget.addWidget(self.page2)
        self.stacked_widget.addWidget(self.car_plate)
        self.stacked_widget.addWidget(self.face_input)
        self.stacked_widget.addWidget(self.face_identity)
        self.stacked_widget.addWidget(self.face_input_many)
        self.stacked_widget.addWidget(self.cook_book)
        self.stacked_widget.addWidget(self.user_view)

    def show_login_window(self):
        """登录成功后打开主窗口"""
        dialog = self.login_in_widget  # 创建登录窗口实例，并设置为模态（默认行为）
        dialog.exec_()  # 显示模态对话框，阻塞直到对话框关闭
        if dialog.result() == 1 and len(self.user) > 0:
            self.show()
            print("登录成功")

    @pyqtSlot(str)
    def receive_value(self, value):
        """用户登录成功返回的用户名"""
        self.user = value
        print(self.user)

    def put_window_in_center(self):
        """窗口居中"""
        # 获取屏幕的中心点
        screen = QApplication.primaryScreen()
        screen_geometry = screen.geometry()
        screen_center = screen_geometry.center()

        # 获取窗口的尺寸
        window_geometry = self.geometry()
        window_width = window_geometry.width()
        window_height = window_geometry.height()

        # 计算窗口居中时的左上角坐标
        x = int(screen_center.x() - window_width / 2)
        y = int(screen_center.y() - window_height / 2)

        # 设置窗口的位置
        self.move(x, y)

        self.setWindowTitle('Centered Window')

    def center(self):
        """窗口居中"""
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

    def page_change(self, page, width=640, height=320):
        self.resize(width, height)
        self.center()
        self.stacked_widget.setCurrentIndex(page)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainWin = MainWindow()
    mainWin.show()
    sys.exit(app.exec_())
